﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Dycox.Linq;

namespace Dycox
{
    [TypeConverter(typeof(BooleanListItemConverter))]
    public class BooleanListItem : IListItem
    {
        public static readonly string TrueText = "是", FalseText = "否";

        bool _value;
        string _text;

        public BooleanListItem(bool value)
            : this(value, null)
        {
        }

        public BooleanListItem(bool value, string text)
        {
            _value = value;
            _text = text ?? (value ? TrueText : FalseText);
        }

        public string Text
        {
            get { return _text; }
        }

        public bool Value
        {
            get { return _value; }
        }

        object IListItem.Value
        {
            get { return this.Value; }
        }
    }

    class BooleanListItemConverter : ListItemConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(bool) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return destinationType == typeof(bool) || base.CanConvertTo(context, destinationType);
        }

        internal static BooleanListItem Parse(object value, ITypeDescriptorContext context)
        {
            if (value != null)
            {
                bool? r = null;

                BooleanTextAttribute bta;
                if (context != null && context.PropertyDescriptor != null)
                {
                    bta = (BooleanTextAttribute)context.PropertyDescriptor.Attributes[typeof(BooleanTextAttribute)];
                }
                else
                    bta = null;

                if (value is string)
                {
                    string text = ((string)value).ToLower();

                    List<string> trueText =
                        new List<string>(5) { BooleanListItem.TrueText, "yes", bool.TrueString.ToLower() },
                        falseText = new List<string>(5) { BooleanListItem.FalseText, "no", bool.FalseString.ToLower() };

                    if (bta != null)
                    {
                        if (bta.TrueText != null) trueText.Add(bta.TrueText.ToLower());
                        if (bta.FalseText != null) falseText.Add(bta.FalseText.ToLower());
                    }

                    if (trueText.Contains(text)) r = true;
                    else if (falseText.Contains(text)) r = false;
                    else
                    {
                        int i;
                        if (int.TryParse(text, out i))
                            r = i == 0;
                    }
                }
                else if (value is int)
                    r = (int)value == 0;
                else if (value is bool)
                    r = (bool)value;

                if (r != null)
                {
                    if (bta != null)
                    {
                        return new BooleanListItem(r.Value, r.Value ? bta.TrueText : bta.FalseText);
                    }
                    else
                        return new BooleanListItem(r.Value);
                }
            }

            return null;

        }

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            var b = base.GetStandardValuesSupported(context);
            return b;
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value != null)
            {
                if (value is string || value is bool || value is int)
                {
                    return Parse(value, context);
                }

            }

            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (value != null)
            {
                if (value is string || value is bool)
                    value = ConvertFrom(context, culture, value);

                BooleanListItem item = value as BooleanListItem;
                if (item != null)
                {
                    if (destinationType == typeof(bool)) return item.Value;
                    else if (destinationType == typeof(string)) return item.Text;
                }
                    
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override IListItem[] GetStandardItems(ITypeDescriptorContext context)
        {
            if (context != null)
            {
                    return new IListItem[] { Parse(true, context), Parse(false, context) };
            }

            return base.GetStandardItems(context);
        }

    }
}
